/**
 * 
 */
package com.hfjyz.exam.service;

import javax.servlet.http.HttpServletRequest;

import org.springframework.validation.BindException;

import com.hfjyz.exam.command.AdminLoginCommand;
import com.hfjyz.exam.command.LoginCommand;
import com.hfjyz.exam.command.ModifyPasswordCommand;
import com.hfjyz.exam.contants.ExamContants;
import com.hfjyz.exam.dao.AdminDao;
import com.hfjyz.exam.dao.UserDao;
import com.hfjyz.exam.model.Admin;
import com.hfjyz.exam.model.UserInfo;
import com.hfjyz.exam.util.CryptUtil;

/**
 *
 */
public class LoginServiceImpl extends AbstractServiceImpl implements
		LoginService {
	private UserDao userDao;
	private AdminDao adminDao;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.hfjyz.exam.service.LoginService#login(javax.servlet.http.
	 * HttpServletRequest, javax.servlet.http.HttpServletResponse,
	 * com.hfjyz.exam.command.LoginCommand,
	 * org.springframework.validation.BindException)
	 */
	@Override
	public UserInfo login(HttpServletRequest request, LoginCommand command,
			BindException errors) throws Exception {

		UserInfo u = userDao.getByIdCardNo(command.getUsername());
		if (u == null
				|| !CryptUtil.encrypt(command.getPassword()).equals(
						u.getPassword())) {
			errors.rejectValue("username", "error.login.wrangnameorpass",
					"failed login! confirm your username or password.");
			return null;
		}
		return u;

	}

	@Override
	public void modifyPassword(HttpServletRequest request,
			ModifyPasswordCommand cmd, BindException errors) throws Exception {

		UserInfo userInfo = (UserInfo) request.getSession().getAttribute(
				ExamContants.SESSION_KEY_LOGIN_INFO);
		UserInfo u = userDao.get(userInfo.getId());
		if (u == null) {
			errors.reject("error.modifypassword.idCardNo.invalid", "身份证号不正确!");
			return;
		}
		String encryPass = CryptUtil.encrypt(cmd.getOldPassword());
		if (!encryPass.equals(u.getPassword())) {
			errors.rejectValue("oldPassword",
					"error.modifypassword.oldpassword.invalid", "旧密码不正确!");
			return;
		}
		u.setPassword(CryptUtil.encrypt(cmd.getPassword()));
		userDao.update(u);

	}

	@Override
	public void adminLogin(HttpServletRequest request, AdminLoginCommand cmd,
			BindException errors) throws Exception {

		String adminId = cmd.getAdminId();
		String password = cmd.getAdminPassword();
		Admin admin = adminDao.getByName(adminId);
		if (admin == null) {
			errors.rejectValue("adminId", "error.adminlogin.wrangnameorpass",
					"管理员帐号不存在！");
			return;
		}
		String encryPass = CryptUtil.encrypt(password);
		if (!encryPass.equals(admin.getPassword())) {
			errors.rejectValue("adminPassword",
					"error.adminlogin.wrangnameorpass", "密码不正确！");
			return;
		}
	}

	/**
	 * @return the userDao
	 */
	public UserDao getUserDao() {
		return userDao;
	}

	/**
	 * @param userDao
	 *            the userDao to set
	 */
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	/**
	 * @return the adminDao
	 */
	public AdminDao getAdminDao() {
		return adminDao;
	}

	/**
	 * @param adminDao
	 *            the adminDao to set
	 */
	public void setAdminDao(AdminDao adminDao) {
		this.adminDao = adminDao;
	}

}
